import Excel from 'exceljs';
import FileLogger from '../file-logger';

const logger = new FileLogger('utils/excel/createExcel');

export function createExcelSheet(
  sheetName: string,
  sheetHeader: { header: string; key: string; width?: number }[],
  headerColor = '00000000',
) {
  try {
    const wb = new Excel.Workbook();
    wb.created = new Date();
    wb.creator = '系统创建';
    const worksheet = wb.addWorksheet(sheetName, {
      views: [{ state: 'frozen', xSplit: 0, ySplit: 1 }],
    });
    worksheet.columns = sheetHeader?.map((item) => ({
      header: item.header,
      key: item.key,
      width: item.width === undefined ? 20 : item.width,
      style: {
        alignment: { vertical: 'middle', horizontal: 'center' },
        font: { font: 12, family: 3 },
      },
    }));
    const header = worksheet.getRow(1);
    header.height = 24;
    header.font = {
      bold: true,
      size: 14,
      family: 3,
      color: { argb: headerColor },
    };
    return {
      workbook: wb,
      worksheet,
    };
  } catch (err) {
    logger.printError(createExcelSheet.name, err);
    return null;
  }
}
